{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13a53f07-4f13-4f97-a058-54b07610484a",
   "metadata": {},
   "source": [
    "# 特征工程\n",
    "维基百科中给特征工程做出了简单定义：特征工程是利用数据领域的相关知识来创建能够使机器学习算法达到最佳性能的特征的过程<br>\n",
    "\n",
    "Feature Engineering is the process of transforming raw data into features that better represent the underlying problem to the predictive models，resulting in improved model accuracy on unseen data.<br>\n",
    "\n",
    "简而言之，特征工程就是一个把原始数据转变成特征的过程，这些特征可以很好的描述这些数据，并且利用它们建立的模型在未知数据上的表现性能可以达到最优(或者接近最佳性能)<br>\n",
    "\n",
    "特征工程包括数据采集、数据清洗、特征提取、缺失值处理、数据变换、数据编码、无量纲处理、特征选择、降维等一系列步骤\n",
    "49936选择# 特征提取工程进行到特征选择这一步骤，基本上需要对已有业务相关数据进行目标因变量相关性分析；<br>\n",
    "我们可以在训练模型前对自变量和因变量进行相关性分析，也可以对较多的模型进行降维处理；<br>\n",
    "或者在训练模型后利用模型的可解释性对变量的贡献程度进行论证<br>\n",
    "在本文中，我们重点介绍两个相关性分析方法和模型解释算法<br>\n",
    "关于降维的方法，在这篇博文中已有较为详细的解释和代码示例:\n",
    "\n",
    "# 相关性分析\n",
    "## 皮尔逊相关系数\n",
    "在使用皮尔逊相关系数时我们需要注意他的基本假设，该系数只能判断自变量和因变量之间是否存在线性关系:\n",
    "\n",
    "$$\n",
    "    \\rho_{X,Y} = \\frac{E\\big[(X-\\mu_{X})(Y-\\mu_{Y}))\\big]}{\\sigma_{X} \\sigma_{Y}} \n",
    "$$\n",
    "\n",
    "皮尔逊相关系数$\\rho_{X,Y}$的取值范围在$(-1,1)$之间，0代表二者没有线性关系，越接近于0表示线性关系越弱;<br>\n",
    "+1或越接近+1代表二者呈现越强的正向线性关系，反之则是负向线性关系\n",
    "\n",
    "## 皮尔逊相关系数 - python实现\n",
    "scipy.stats 包的 pearsonr函数能够提供计算相关系数的功能<br>\n",
    "该函数同时会对皮尔逊相关系数做$H_0$为：二者为随机分布的假设检验，<br>当p值极小时，我们选择拒绝原假设，进而认同二者具有线性关系\n",
    "简单来说，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75c125bb-08b4-4841-95c2-eba92ea7979e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1st attribute's \n",
      "pearson correlation: 0.7825612318100811, p: 2.890478352614215e-32\n",
      "2nd attribute's \n",
      "pearson correlation: -0.4266575607811239, p: 5.201563255177591e-08\n",
      "3rd attribute's \n",
      "pearson correlation: 0.949034699008389, p: 4.201873152959405e-76\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from scipy.stats import pearsonr\n",
    "\n",
    "X, y = load_iris(return_X_y=True)\n",
    "coef, p_value = pearsonr(X[:,0], y)\n",
    "print(f\"1st attribute's \\npearson correlation: {coef}, p: {p_value}\")\n",
    "coef, p_value = pearsonr(X[:,1], y)\n",
    "print(f\"2nd attribute's \\npearson correlation: {coef}, p: {p_value}\")\n",
    "coef, p_value = pearsonr(X[:,2], y)\n",
    "print(f\"3rd attribute's \\npearson correlation: {coef}, p: {p_value}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "808c13df-e717-495b-909c-adedb4761309",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
